package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 17:53
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CategoryService categoryService;

    //http://localhost:8083/admin/brand/list?page=1&limit=20&
    // id=1&name=xx
    // &sort=add_time&order=desc

    /**
     * 分页查询品牌信息
     * @param id 根据品牌上ID查找
     * @param name 根据品牌商名称查找
     * @param page 当前页
     * @param limit 每页显示数据条数
     * @param sort 排序字段
     * @param order 排序方式
     * @return
     */
    @GetMapping("/list")
    public Object brand(Integer id, String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "20") Integer limit,
                        @RequestParam(defaultValue = "add_time") String sort,
                        @RequestParam(defaultValue = "desc") String order){

        IPage<DtsBrand> iPage = brandService.queryBrandPage(id, name, page, limit, sort, order);
        //获取结果集
        List<DtsBrand> list = iPage.getRecords();
        //获取总数
        long total = iPage.getTotal();
        //封装
        Map<String ,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);

        return ResponseUtil.ok(data);
    }

    @GetMapping("/catAndAdmin")
    public Object deploy(){
        //查询到的管理员的列表
        List<DtsAdmin> adminList = adminService.userList();

        //这个是用来存放最后adminList的value
        List<Map<String,Object>> userList = new ArrayList<>();

        for (DtsAdmin admin : adminList) {
            //循环获得管理员的id和name放到对应的map中
            Map<String,Object> map = new HashMap<>();
            map.put("value", admin.getId());
            map.put("label", admin.getUsername());
            userList.add(map);
        }



        //查询商品返回一级类目列表
        List<DtsCategory> categoryList1 = categoryService.categoryList1();
        //这个list是存放最后的categoryList的value值
        List<CatVo> categoryList = new ArrayList<>();
        //开始循环一级类目列表
        for (DtsCategory dtsCategory : categoryList1) {
            //存放一级类的map
            Map<String,Object> categoryMap1 = new HashMap<>();
            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());
            //查找到的二级类列表
            List<DtsCategory> list = categoryService.categoryList2(dtsCategory.getId());

            List<CatVo> childrenList = new ArrayList<>();
            //把二级类存放到一个大的list中
            for (DtsCategory category2 : list) {
                //children的map1存放键值对
                CatVo catVo2 = new CatVo();
                catVo2.setValue(category2.getId());
                catVo2.setLabel(category2.getName());
                childrenList.add(catVo2);
            }
            catVo.setChildren(childrenList);

            categoryList.add(catVo);
        }

        Map<String,Object> data = new HashMap<>();
        data.put("adminList", userList);
        data.put("categoryList",categoryList);
        return ResponseUtil.ok(data);
    }


    @PostMapping("/create")
    public Object create(@RequestBody DtsBrand brand){
        DtsBrand dtsBrand = brandService.add(brand);

        return ResponseUtil.ok(dtsBrand);
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        int count = brandService.update(brand);
        //修改失败
        if (count == 0){
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok(brand);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        int count = brandService.delete(brand.getId());
        if (count == 0){
            return  ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }



}
